<!-- 入库 -->
<template>
  <div class="init-house">
    <van-nav-bar @click-right="onClickRight" safe-area-inset-top style="background: #3e96e8">
      <template #title>
        <span style="color: #fff">{{title}}</span>
      </template>
      <template #right>
        <span style="color: #fff" @click="save">保存</span>
      </template>
    </van-nav-bar>

    <!-- 表单 -->
    <div class="form">
      <van-form>
        <van-field v-model="values" name="value" label="基础信息" placeholder="" readonly />
        <div>
      
            <table class="table" style="width:100%">
              <tr>
                <td>分类</td>
                <td>品牌</td>
                <td>名称</td>
                <td>单位</td>
                <td>规格</td>
                <td>重量</td>
              </tr>
              <tr v-for="(item,index) in list" :key="index">
                <td>{{item.type}}</td>
                <td>{{item.brand}}</td>
                <td>{{item.name}}</td>
                <td>{{item.unit}}</td>
                <td>{{item.spec}}</td>
                <td>{{item.weight}}</td>
              </tr>

            </table>




        </div>
        <van-field readonly clickable name="datetime" :value="value" label="入库日期" placeholder="点击选择时间"
          @click="showPicker = true" />
        <van-popup v-model="showPicker" position="bottom">
          <van-datetime-picker type="datetime" @confirm="onConfirm" @cancel="showPicker = false" />
        </van-popup>
        <van-field v-model="monmey" type="number" name="monmey" label="合计金额" placeholder="合计金额" />
      </van-form>
      <div class="btns">
        <van-button plain type="info" icon="plus" class="btn" @click="choose">选择物料</van-button>
        <!-- <van-button plain type="default" icon="search" class="btn">入库记录</van-button> -->
      </div>
    </div>
  </div>
</template>

<script>
import Bus from '../../utils/bus.js'
import { warehouseAdd } from "../../api/warehouse";
export default {
  name: "",
  data() {
    return {
      values: "",
      value: "",
      monmey: "",
      title: "",
      showPicker: false,
      list: [],
      type: "",
    };
  },
  methods: {
    onConfirm(time) {
      this.value = this.$moment(time).format("YYYY-MM-DD HH:MM:SS");
      this.showPicker = false;
    },
    onClickRight() { },
    choose() {
      this.$router.push({ path: "/add-material", query: {} });
    },
    Bus() {

      Bus.$on('addgoods', (data) => {

        console.log("addgoods", data);

        this.list.push(data);

      })

    },
    async save() {
      var data = {};
      data.goods = JSON.stringify(this.list);
      data.time = this.value;
      data.money = this.monmey;
      data.userid = localStorage.getItem("userid");
      data.type = this.$route.query.type;
      let res = await warehouseAdd(data);
      if (res.status == 200) {

        this.$toast(res.message);
        this.$router.back();

      }

    }
  },
  computed: {},
  watch: {},
  created() {

    this.title = this.$route.query.title;
    this.type = this.$route.query.type;
  },
  beforeMount() {
    this.Bus()
  },
  mounted() {


  },
  destroyed() {
    console.log("什么情况", this.type);
    Bus.$emit('warehouse', this.type);
  },

};
</script>

<style lang="scss" scoped>
.init-house {
  width: 100%;
  height: 100%;

  .form {
    width: 100%;
    background-color: #fff;
    padding: 40px 30px;

    .btns {
      margin-top: 100px;
      width: 100%;
      display: flex;
      justify-content: space-around;

      .btn {
        border-radius: 15px;
      }
    }
  }
}
</style>